package com.axend.aerosense.room.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.CommonModuleInit;
import com.axend.aerosense.common.bean.VipFunSetBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.ui.activity.RoomVipFunActivity;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.disposables.Disposable;

/**
 * Author: ChenHongWei
 * Date: 2021/3/27 11:19
 */
public class RoomVipFunViewModel extends BaseViewModel {

    public MutableLiveData<Boolean> isChecked = new MutableLiveData<>(false);

    public MutableLiveData<Boolean> isShowFragment = new MutableLiveData<>(true);

    private String token = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
    private Disposable disposable;


    //保存设置
    public void saveSettings(RoomVipFunActivity activity, VipFunSetBean bean, String url){
        disposable = EasyHttp.post(url)
                .headers("token", token)
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(bean))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        dismissProgress();
                        ToastUtils.showShort(R.string.room_set_success);
                        activity.onBackPressed();
                    }

                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        ToastUtils.showShort(R.string.common_login_first);
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                        ToastUtils.showShort(CommonModuleInit.getmContext().getString(R.string.room_set_fail) + ", " + e.getMessage());
                        dismissProgress();
                        activity.onBackPressed();
                    }
                });
    }

    //用餐分析
//    public void setHaveMeals(RoomVipFunActivity activity, VipFunSetBean bean){
//        disposable = EasyHttp.post(UrlApi.URL.SetHaveMeals.getUrl())
//                .headers("token", token)
//                .cacheMode(CacheMode.NO_CACHE)
//                .upJson(JsonUtil.toJson(bean))
//                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
//                    @Override
//                    public void onSuccess(BaseStringBean baseStringBean) {
//                        dismissProgress();
//                        ToastUtils.showShort(R.string.room_set_success);
//                        activity.onBackPressed();
//                    }
//
//                    @Override
//                    public void onTokenExpire() {
//                        dismissProgress();
//                    }
//
//                    @Override
//                    public void onError(ApiException e) {
//                        ToastUtils.showShort(R.string.room_set_fail);
//                        dismissProgress();
//                        e.printStackTrace();
//                    }
//                });
//    }

    //作息规律
//    public void setWorkAndRest(RoomVipFunActivity activity, VipFunSetBean bean){
//        disposable = EasyHttp.post(UrlApi.URL.SetWorkAndrest.getUrl())
//                .headers("token", token)
//                .cacheMode(CacheMode.NO_CACHE)
//                .upJson(JsonUtil.toJson(bean))
//                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
//                    @Override
//                    public void onSuccess(BaseStringBean baseStringBean) {
//                        dismissProgress();
//                        ToastUtils.showShort(R.string.room_set_success);
//                        activity.onBackPressed();
//                    }
//
//                    @Override
//                    public void onTokenExpire() {
//                        dismissProgress();
//                    }
//
//                    @Override
//                    public void onError(ApiException e) {
//                        ToastUtils.showShort(R.string.room_set_fail);
//                        dismissProgress();
//                        e.printStackTrace();
//                    }
//                });
//    }

    //趋势分析
//    public void setTrendAnalysis(RoomVipFunActivity activity, VipFunSetBean bean){
//        disposable = EasyHttp.post(UrlApi.URL.SetTrendAnalysis.getUrl())
//                .headers("token", token)
//                .cacheMode(CacheMode.NO_CACHE)
//                .upJson(JsonUtil.toJson(bean))
//                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
//                    @Override
//                    public void onSuccess(BaseStringBean baseStringBean) {
//                        dismissProgress();
//                        ToastUtils.showShort(R.string.room_set_success);
//                        activity.onBackPressed();
//                    }
//
//                    @Override
//                    public void onTokenExpire() {
//                        dismissProgress();
//                    }
//
//                    @Override
//                    public void onError(ApiException e) {
//                        ToastUtils.showShort(R.string.room_set_fail);
//                        dismissProgress();
//                        e.printStackTrace();
//                    }
//                });
//    }

    @Override
    public void onLoad() {

    }

    @Override
    protected void onCleared() {
        if(null != disposable && !disposable.isDisposed()){
            disposable.dispose();
            disposable = null;
        }
        super.onCleared();
    }
}
